package com.fanghaoran.service;

import com.fanghaoran.bean.Interaction;
import com.fanghaoran.dao.ManagerDao;
import com.fanghaoran.entity.Book;
import com.fanghaoran.entity.Words;

import java.io.*;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: win11
 * @Date: 2023/03/27/22:40
 * @Description:
 */
public class ManagerService {
    /**
     * 数据库对象
     */
    private final ManagerDao managerDao = new ManagerDao();

    //管理员获取用户信息
    /**
     *管理员获取全部用户信息
     */
    public Interaction getUserInfo() throws Exception {
        return managerDao.getUserInfo();
    }

    /**
     *管理员通过用户名获取特定用户信息
     */
    public Interaction getUserInfoByUsername(String username) throws Exception {
        return managerDao.getUserInfoByUsername(username);
    }

    /**
     *管理员通过电话号码获取特定用户信息
     */
    public Interaction getUserInfoByTelephone(String telephone) throws Exception {
        return managerDao.getUserInfoByTelephone(telephone);
    }

    /**
     *管理员通过昵称获取特定用户信息
     */
    public Interaction getUserInfoByNickname(String nickname) throws Exception {
        return managerDao.getUserInfoByNickname(nickname);
    }

    /**
     *管理员拉黑用户
     */
    public void updateBlocked(Long newValue, String username) throws SQLException {
        managerDao.updateBlocked(newValue, username);
    }



    /**
     * 搜索某本单词书的所有单词
     */
    public Interaction selectWordOfBook(String bookName) throws Exception {
        return managerDao.selectWordOfBook(bookName);
    }

    /**
     * 添加单词(1.添加到单词库 2.添加到单词书)
     */
    public Interaction insertWord(String word, String translation, String bookName) throws Exception {
        //先对输入框进行判空
        if (word.length() == 0 || translation.length() == 0) {
            return new Interaction("输入为空", null);
        }
        //返回该单词在单词表中的存在情况
        @SuppressWarnings("unchecked")
        List<Words> wordsList = (List<Words>) managerDao.selectWord(word).getMessage();
        //返回该单词在该单词书中的存在情况
        @SuppressWarnings("unchecked")
        List<Words> wordsList2 = (List<Words>) managerDao.selectWord(word, bookName).getMessage();
        //根据输入 查询该单词在数据库的存储情况
        if (!wordsList.isEmpty() && wordsList2.isEmpty()) {
            //1.在单词数据中存在,但是在该单词书中不存在->只需要插入单词-书中间表
            managerDao.insertWordToBook(word, bookName);
            return new Interaction("添加成功", null);
        } else if (wordsList.isEmpty()) {
            //2.在单词数据中不存在->需要插入单词数据表+单词-书中间表
            managerDao.insertWordToWord(word, translation);
            managerDao.insertWordToBook(word, bookName);
            return new Interaction("添加成功", null);
        } else {
            //3.该单词在该书中已存在
            return new Interaction("该单词在该单词书中已存在", null);
        }
    }

    /**
     * 删除某本单词书中的单词(只是删除该单词和该单词书的中间表记录)
     *
     * @param word     单词
     * @param bookName 释义
     */
    public Interaction deleteWordFromBook(String word,String bookName) throws Exception {
        //先对输入框判空
        if(word.length()==0){
            return new Interaction("输入为空",null);
        }
        //返回该单词在该单词书中的存在情况
        @SuppressWarnings("unchecked")
        List<Words> wordsList = (List<Words>) managerDao.selectWord(word, bookName).getMessage();
        if(!wordsList.isEmpty()){
            //单词在单词书中存在,可以删除中间表记录
            managerDao.deleteWordFromBook(word,bookName);
            return new Interaction("删除成功",null);
        }else{
            return new Interaction("该单词不在这本单词书中",null);
        }
    }

    /**
     * 修改单词库中的单词
     *
     * @param word        修改后的单词
     * @param translation 修改后的释义
     * @param preWord     修改前的单词
     */
    public Interaction modifyWord(String word,String translation,String preWord) throws SQLException {
        //先对输入框判空
        if(word.length()==0||translation.length()==0){
            return new Interaction("输入为空",null);
        }
        //修改
        managerDao.modifyWord(word,translation,preWord);
        return new Interaction("修改成功",null);
    }


    //单词书的添加和查看

    /**
     * 获取单词书列表
     */
    public Interaction getBookInfo() throws Exception {
        return managerDao.getBookInfo();
    }

    /**
     * 添加书籍
     *
     * @param bookName 书籍的名字
     */
    public Interaction insertBook(String bookName) throws Exception {
        //先对输入框判空
        if(bookName.length()==0){
            return new Interaction("输入为空",null);
        }
        //返回该单词书在单词书表中的存在情况
        @SuppressWarnings("unchecked")
        List<Book> bookList = (List<Book>) managerDao.selectBook(bookName).getMessage();
        if(bookList.isEmpty()){
            //该单词书在单词书表中不存在,可以添加
            managerDao.insertBook(bookName);
            return new Interaction("添加成功",null);
        }else{
            return new Interaction("该书已存在",null);
        }
    }


    /**
     * 展示公告
     */
    public Interaction readNotice() throws IOException {
        StringBuilder sb=new StringBuilder();
        BufferedReader br=new BufferedReader(new FileReader("src\\com\\fanghaoran\\notice"));
        String line;
        while ((line=br.readLine())!=null){
            sb.append(line);
            sb.append("\n");
        }
        String s = sb.toString();
        br.close();
        return new Interaction(s,null);
    }

    /**
     * 编辑修改公告
     */
    public void writeNotice(String text) throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("src\\com\\fanghaoran\\notice"));
        bw.write(text);
        bw.close();
    }
}
